package com.ruoyi.project.service.email;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class VerificationCodeService {
        // 存储每个邮箱对应的验证码
        private final Map<String, String> verificationCodes = new HashMap<>();

        /**
         * 生成一个随机验证码
         * @return 验证码
         */
        public String generateVerificationCode() {
            Random random = new Random();
            int code = random.nextInt(900000) + 100000;  // 生成6位验证码
            return String.valueOf(code);
        }

        /**
         * 存储验证码
         * @param email 邮箱地址
         * @param verificationCode 验证码
         */
        public void storeVerificationCode(String email, String verificationCode) {
            verificationCodes.put(email, verificationCode);
        }

        /**
         * 验证验证码
         * @param email 用户邮箱
         * @param code 用户输入的验证码
         * @return 验证结果
         */
        public boolean verifyCode(String email, String code) {
            String storedCode = verificationCodes.get(email);
            return storedCode != null && storedCode.equals(code);
        }
    }

